package org.example.topicdata;

import org.example.ConnectDatabase;

import java.sql.PreparedStatement;
import java.sql.SQLException;

public class AddTopic extends ConnectDatabase {

    int isOK = 1;
    public int insertTopic(Topic topic){
        new ConnectDatabase();
        try {
            String SQL ="insert into Topics values(?,?,?,?,?,?,?)";
            PreparedStatement sta  = con.prepareStatement(SQL);
            //从左向右数第1个通配符?的值是topic.getID():
            sta.setInt(1, topic.getID());
            sta.setString(2, topic.getTopicName());
            sta.setString(3, topic.getSelect_A() );
            sta.setString(4, topic.getSelect_B() );
            sta.setString(5, topic.getSelect_C() );
            sta.setString(6, topic.getSelect_D() );
            //从左向右数第7个通配符?的值是topic.getAns():
            sta.setString(7, topic.getAns());
            isOK = sta.executeUpdate();
            con.close();
        }
        catch(SQLException e) {
            isOK = 0;                //Topics表中ID字段是主键，即不允许ID重复
        }
        return isOK;
    }
}
